package gdut.jmxywz.action;

import gdut.jmxywz.been.Bookmaking;
import gdut.jmxywz.been.BookmakingFile;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.ServletActionContext;



@SuppressWarnings("serial")
public class BookmakingFilesAction extends BaseAction {
	
	private String  bookmakingId;
	private String  bookmakingFileId;
	private String  bookmakingFileName;
	private String  bookmakingFileContext;
	private String  bookmakingFileUrl;
	private List<BookmakingFile>  bookmakingFile;
	private BookmakingFile bookmakingFile2;
	

	/***
	 * Delete 删除附件
	 */
	
	@SuppressWarnings({ "unchecked", "deprecation" })
	public String  Delete() throws Exception{
		
 
        int id = Integer.parseInt(bookmakingId);
        int filesId=0;
     
//        boolean b = this.getCommonService().deleteAllFile(id,filesId);
         boolean b = false;
         
         String hql = "from BookmakingFile where bookmakingId="+id;
         System.out.println("=+++++++ "+bookmakingFileId);
         if(!"".equals(bookmakingFileId)&&bookmakingFileId!=null){
         	filesId = Integer.parseInt(bookmakingFileId);
         	
         	hql = hql + " and bookmakingFileId = "+filesId;
         }
         String []parem = null;
         
         System.out.println("-------hql------"+hql);
         List i = this.getCommonService().findListByHql(hql, parem);
         
         Iterator iter = i.iterator();
         
         while(iter.hasNext()){
        	 
        	 BookmakingFile bookmakingfiles = (BookmakingFile)iter.next();
        	 
        	 System.out.println("++准备删除文件++");
        	 b=this.getCommonService().deleteObject(bookmakingfiles);
        	 
        
        	 System.out.println("-------------删除成功--------------");
        	 
	         String path =ServletActionContext.getRequest().getRealPath("/");
	        
	         System.out.println("----------------"+path);
	         
	         path =path+bookmakingfiles.getBookmakingFileUrl();
	         
	         System.out.println("++++"+path);
	         
	         boolean b1 = this.deleteFile(path);//输入路径，调用deletefile（）将文件从服务器中删除
	         if(b1){
	        		System.out.println("---从服务器上文件删除成功！");
	        		
	        	}else{
	        		System.out.println("----从服务器上文件删除失败！");
	        		
	        	}   	 
        	 
        	 
         }
         
         
		if(b){
			return "BookmakingFilesDelete";
		}
		else{
			return "error";
		}
         
	}
	
	/***
	 * preUpdate 显示将要删除附件的信息
	 */
	
	public  String PreUpdate() throws Exception{
		 
			int  id  = 0 ;
			
			if(!"".equals(bookmakingFileId)&&bookmakingFileId!=null){
				
				id = Integer.parseInt(bookmakingFileId);
			}
			
			bookmakingFile2 =(BookmakingFile) this.getCommonService().findByIntId(id, "gdut.jmxywz.been.BookmakingFile");
			
//			System.out.println("++++++++++++++++++++++++++++++++++");
//			
//			System.out.println("========="+bookmakingFile2.getBookmakingFileName());
//			
//			Bookmaking bookmaking = new Bookmaking();
//			bookmaking =bookmakingFile2.getBookmaking();
//			
//			System.out.println("-------rrr--------"+bookmaking.getBookmakingTitle());
//			
		
		return "BookmakingFilesPreUpdate";
	}
	
	
	
	
	
	/***
	 * 
	 * update 修改部件
	 */
	
	
	public String  Update() throws Exception{
		
		int  fileid  = 0 ;
		int  infId = 0; 
		
		BookmakingFile bookmakingFiles = new  BookmakingFile();
		
		if(!"".equals(bookmakingFileId)&&bookmakingFileId!=null&&!"".equals(bookmakingId)&&bookmakingId!=null){
			
			fileid = Integer.parseInt(bookmakingFileId);
			
			infId = Integer.parseInt(bookmakingId);
			
			Bookmaking bookmaking  = (Bookmaking)this.getCommonService().findByIntId(infId, "gdut.jmxywz.been.Bookmaking");
			
			bookmakingFiles.setBookmakingFileId(fileid);
			bookmakingFiles.setBookmakingFileContext(bookmakingFileContext);
			bookmakingFiles.setBookmakingFileName(bookmakingFileName);
			bookmakingFiles.setBookmakingFileUrl(bookmakingFileUrl);
		    
			bookmakingFiles.setBookmaking(bookmaking);
		}
		
	    boolean b= this.getCommonService().updateObject(bookmakingFiles);
		
	    if(b){
	    	
	    	System.out.println("附件信息修改成功！");
	    	
	    	return "BookmakingFilesUpdate";
	    }else{
	    	
	    	System.out.println("附件信息修改失败！");
	    	
	    	return "error";
	    }
		
	}

//	
//	public  boolean DeleteFiles(String sPath) {   
//		boolean  flag = false;   
//		File   file = new File(sPath);   
//	    // 路径为文件且不为空则进行删除   
//	    if (file.isFile() && file.exists()) {   
//	        file.delete();   
//	        System.out.println("---成功从服务器删除文件---");
//	        flag = true;   
//	    }   
//	    return flag;   
//	}  
//	
	
	
	
	   /**
	    *  deleteFile  从服务器上删除附件
	    * @param sPath
	    * @return
	    */
		
		public  boolean deleteFile(String sPath) {   
			boolean  flag = false;   
			File   file = new File(sPath);   
		    // 路径为文件且不为空则进行删除   
		    if (file.isFile() && file.exists()) {   
		        file.delete();   
		        System.out.println("---成功从服务器删除文件---");
		        flag = true;   
		    }   
		    return flag;   
		}

		
		
	public String getBookmakingId() {
		return bookmakingId;
	}

	public void setBookmakingId(String bookmakingId) {
		this.bookmakingId = bookmakingId;
	}

	public String getBookmakingFileId() {
		return bookmakingFileId;
	}

	public void setBookmakingFileId(String bookmakingFileId) {
		this.bookmakingFileId = bookmakingFileId;
	}

	public String getBookmakingFileName() {
		return bookmakingFileName;
	}

	public void setBookmakingFileName(String bookmakingFileName) {
		this.bookmakingFileName = bookmakingFileName;
	}

	public String getBookmakingFileContext() {
		return bookmakingFileContext;
	}

	public void setBookmakingFileContext(String bookmakingFileContext) {
		this.bookmakingFileContext = bookmakingFileContext;
	}


	public String getBookmakingFileUrl() {
		return bookmakingFileUrl;
	}

	public void setBookmakingFileUrl(String bookmakingFileUrl) {
		this.bookmakingFileUrl = bookmakingFileUrl;
	}



	public List<BookmakingFile> getBookmakingFile() {
		return bookmakingFile;
	}

	public void setBookmakingFile(List<BookmakingFile> bookmakingFile) {
		this.bookmakingFile = bookmakingFile;
	}

	public BookmakingFile getBookmakingFile2() {
		return bookmakingFile2;
	}

	public void setBookmakingFile2(BookmakingFile bookmakingFile2) {
		this.bookmakingFile2 = bookmakingFile2;
	}


		
		
		
}
